﻿//----------------------------------------------------------------------------------------------------
// The IAIPlayer is the base class for all AI types
//----------------------------------------------------------------------------------------------------

package HG.AI
{
	import HG.Events.NodeEvent;
	import HG.Events.LevelEvent;
	
	import HG.Visual.VisualNode;
	
	import HG.Actions.IAction;
	
	import HG.GameService;
	import HG.Managers.LevelManager;
	import HG.Managers.ActionManager;
	import HG.Managers.ResourceManager;
	import HG.Managers.NodeManager;
	import HG.Managers.TurnManager;
	
	public class SimpleAIPlayer extends IAIPlayer
	{
		var m_levelManager:LevelManager;
		var m_actionManager:ActionManager;
		var m_resourceManager:ResourceManager;
		var m_turnManager:TurnManager;
		
		var m_myNodes:Array;
		var m_myNodeIDs:Array;
		
		//----------------------------------------------------------------------------------------------------
		public function SimpleAIPlayer(ID:Number)
		{
			super(ID);
			
			m_myNodes = new Array();
			
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_actionManager = GameService.GetInstance().GetActionManager();
			m_resourceManager = GameService.GetInstance().GetResourceManager();
			m_turnManager = GameService.GetInstance().GetTurnManager();
			
			m_levelManager.addEventListener(NodeEvent.CAPTURE, OnNodeCaptured);
			m_levelManager.addEventListener(LevelEvent.LOAD, OnLevelLoadComplete);
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DeltaTime:Number)
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		// External events
		//----------------------------------------------------------------------------------------------------
		private function OnLevelLoadComplete(evt:LevelEvent)
		{
			// Get initial nod elsit form level manager.
			m_myNodes = m_levelManager.GetVisualNodesOcupiedByPlayer(m_playerID);
			
			Debug.Trace(">>" + m_myNodes.length);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnNodeCaptured(evt:NodeEvent)
		{
			var nodeID:Number = evt.m_node.GetID();
			
			if (evt.m_node.GetIsOcupiedBy(m_playerID))
			{
				m_myNodes.push(nodeID);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		// Perform actions is the AI's turn to lodge actions for the next turn. return true when ready.
		//----------------------------------------------------------------------------------------------------
		public override function PerformActions():Boolean
		{
			if (m_myNodes.length <= 0)
				return true;
			
			var nodeChosen = false;
			var timesChecked:Number = 0;
				
			do
			{
				var number:Number = Math.floor(Math.random() * m_myNodes.length);
				var nodeID:Number = m_myNodes[number]
				
				var node:VisualNode = m_levelManager.GetVisualNode(nodeID);
				
				// Determine node to attack
				var nextNodeID:Number = 0;
			
				var connections:Array = node.GetConnections();
				number = Math.floor(Math.random() * connections.length);
				nextNodeID = connections[number];
				
				// Make sure I dont allready own this node.
				var nextNode:VisualNode = m_levelManager.GetVisualNode(nextNodeID);

				if (!nextNode.GetIsOcupiedBy(m_playerID) && nextNode.GetPendingAction() == -1)
					nodeChosen = true;
				
				timesChecked++;
			} 
			while(!nodeChosen && timesChecked < 50);
			
			if (!nodeChosen)
			{
				Debug.Trace ("AI " + m_playerID + " failed to choose a node");
				return true;
			}
			
			// Determine action to perform
			var actionID:Number = ActionManager.ACTION_HACK;
			switch(nextNode.GetType())
			{
				case NodeManager.NODE_DOCUMENT:
				{
					actionID = ActionManager.ACTION_DOWNLOAD;
				}break;
			}
			
			// Check costs and do it.
			var cost:Number = m_actionManager.GetActionCost(actionID);
			
			Debug.Trace("AI: " + m_playerID + " chose action " + actionID + " for node " + nextNodeID);
			
			// no costs for you! hahaha.
			//if (m_resourceManager.CanPlayerAfford(m_playerID, cost))
			//{
				var action:IAction = m_actionManager.CreateAction(
					actionID, 
					m_playerID,
					nextNodeID);
				
				m_turnManager.AddPendingAction(action);
				//m_resourceManager.SpendResource(m_playerID, cost);
			//}
			
			return true;
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}
